﻿//---------------------------------------------------------------------
// Author: Harley Green
//
// Description: Base cmdlet for MSMQ cmdlets
//
// Creation Date: 2008/3/8
//---------------------------------------------------------------------

using System.Management.Automation;
using System.Messaging;

namespace Pscx.Commands.Messaging
{
    public class MessageQueueCmdlet : PSCmdlet
    {
        private string machineName = ".";

        [Parameter(ParameterSetName = "Path")]
        public string Path { get; set; }

        [Parameter(ParameterSetName = "Named")]
        public string MachineName
        {
            get { return machineName; }
            set { machineName = value; }
        }

        [Parameter(ParameterSetName = "Named", Position = 0)]
        public string QueueName { get; set; }

        [Parameter(ParameterSetName = "Named")]
        public SwitchParameter Private { get; set; }

        [Parameter(ParameterSetName = "Queue", ValueFromPipeline = true)]
        public MessageQueueInfo Queue { get; set; }

        protected bool Exists
        {
            get { return MessageQueue.Exists(GetPath()); }
        }

        protected MessageQueue GetQueue()
        {
            return new MessageQueue(GetPath());
        }

        protected string GetPath()
        {
            string path = string.Empty;
            switch (ParameterSetName)
            {
                case "Path":
                    path = Path;
                    break;
                case "Named":
                    path = string.Format(@"{0}\{1}{2}", MachineName, Private.IsPresent ? @"private$\" : "", QueueName);
                    break;
                case "Queue":
                    path = string.Format(@"{0}\{1}", Queue.MachineName, Queue.QueueName);
                    break;
            }
            return path;
        }
    }
}